package com.qiyuesuo.sdk.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiyuesuo.sdk.v2.SdkClient;
import com.qiyuesuo.sdk.v2.bean.Category;
import com.qiyuesuo.sdk.v2.bean.Contract;
import com.qiyuesuo.sdk.v2.bean.Signatory;
import com.qiyuesuo.sdk.v2.bean.TemplateParam;
import com.qiyuesuo.sdk.v2.bean.User;
import com.qiyuesuo.sdk.v2.json.JSONUtils;
import com.qiyuesuo.sdk.v2.param.SignParam;
import com.qiyuesuo.sdk.v2.request.ContractDraftRequest;
import com.qiyuesuo.sdk.v2.request.ContractPageRequest;
import com.qiyuesuo.sdk.v2.request.ContractSignCompanyRequest;
import com.qiyuesuo.sdk.v2.response.ContractPageResult;
import com.qiyuesuo.sdk.v2.response.SdkResponse;

public class SendByCategory {

    private static final Logger logger = LoggerFactory.getLogger(SendByCategory.class);
    private static final String PLATFORM_NAME = "测试222222";

    public static void main(String[] args) throws Exception {
        String url = "https://openapi.qiyuesuo.cn";
        String accessKey = "Mi6E4lLXjj";
        String accessSecret = "XanHE3KZb7jU61JA5H3CJdk8Uv7da0";

        SdkClient client = new SdkClient(url, accessKey, accessSecret);

//        SdkResponse<Contract> draft = draft(client);
//        logger.info("创建合同草稿成功，并发起，合同ID：{}", draft.getResult().getId());
//        companySealSignByCategoryConfig(client, 2674228996668920778L);
//        logger.info("公章签署成功");

        SdkResponse<ContractPageResult> pageResponse = gerenateSignUrl(client, 2674248692592021797L);
        logger.info("生成签署链接成功，签署地址：{}", pageResponse.getResult().getPageUrl());
    }

    /**
     * 根据业务分类配置创建合同草稿并发起合同
     *
     * @param client
     * @return
     * @throws Exception
     */
    private static SdkResponse<Contract> draft(SdkClient client) throws Exception {
        Contract draftContract = new Contract();
        // 基础配置
        draftContract.setSubject("见习代理人合同");
        draftContract.setCategory(new Category(2674186439108858315L)); // 设置业务分类ID,套用该业务分类的配置
		draftContract.setSn("HT66666");// 合同业务ID
		draftContract.setExpireTime("2020-03-15  23:59:59");// 合同过期时间 yyyy-MM-dd HH:mm:ss 格式

		// 业务分类中包含参数模板，设置参数内容
        draftContract.addTemplateParam(new TemplateParam("p1", "参数11111"));
//        draftContract.addTemplateParam(new TemplateParam("p2", "参数2222"));

        // 设置接收方详情，接收方需要与业务分类中设置的签署方流程匹配
        // 公司签署方
        Signatory companySignatory = new Signatory();
        companySignatory.setTenantName(PLATFORM_NAME);// 公司名称
        companySignatory.setTenantType("COMPANY");
        companySignatory.setReceiver(new User("18229781528", "MOBILE"));
//        companySignatory.setSerialNo(1);// 业务模板中 的顺序
        draftContract.addSignatory(companySignatory);

        // 个人签署方
        Signatory personalSignatory = new Signatory();
        personalSignatory.setTenantType("PERSONAL");
        personalSignatory.setReceiver(new User("18229781528", "MOBILE"));
        personalSignatory.setTenantName("唐志强");
//        personalSignatory.setSerialNo(2);
        draftContract.addSignatory(personalSignatory);
        draftContract.setSend(true); // 创建合同草稿并发起合同

        String response = null;
        try {
            response = client.service(new ContractDraftRequest(draftContract));
        } catch (Exception e) {
            throw new Exception("创建合同草稿请求服务器失败，失败原因：" + e.getMessage());
        }
        SdkResponse<Contract> sdkResponse = JSONUtils.toQysResponse(response, Contract.class);
        if (!sdkResponse.getCode().equals(0)) {
            throw new Exception("创建合同草稿失败，失败原因：" + sdkResponse.getMessage());
        }
        return sdkResponse;
    }

    /**
     * 根据业务分类配置的签署位置与印章签署公章
     * 若未配置签署位置需要设置签署位置与印章
     *
     * @param client
     * @param contractId
     * @return
     * @throws Exception
     */
    private static SdkResponse companySealSignByCategoryConfig(SdkClient client, Long contractId) throws Exception {
        SignParam param = new SignParam();
        param.setContractId(contractId);

        String response = null;
        try {
            response = client.service(new ContractSignCompanyRequest(param));
        } catch (Exception e) {
            throw new Exception("公章签署请求服务器失败，失败原因：" + e.getMessage());
        }
        SdkResponse sdkResponse = JSONUtils.toQysResponse(response, Object.class);
        if (!sdkResponse.getCode().equals(0)) {
            throw new Exception("公章签署失败，失败原因：" + sdkResponse.getMessage());
        }
        return sdkResponse;
    }


    /**
     * 生成用户的签署链接
     *
     * @param client
     * @param contractId
     * @return
     * @throws Exception
     */
    private static SdkResponse<ContractPageResult> gerenateSignUrl(SdkClient client, Long contractId) throws Exception {
        User signUser = new User("18229781528", "MOBILE");
        String callbackPage = "https://www.qiyuesuo.cn";
        ContractPageRequest request = new ContractPageRequest(contractId, signUser, callbackPage);

        String response;
        try {
            response = client.service(request);
        } catch (Exception e) {
            throw new Exception("生成签署链接请求服务器失败，失败原因：" + e.getMessage());
        }
        SdkResponse<ContractPageResult> sdkResponse = JSONUtils.toQysResponse(response, ContractPageResult.class);
        if (!sdkResponse.getCode().equals(0)) {
            throw new Exception("法人章签署失败，失败原因：" + sdkResponse.getMessage());
        }
        return sdkResponse;
    }

}
